DIY Exercise 10-1: Handle errors
Time estimate: 3 hours
Objectives
In this exercise, you handle errors in a Mule application. You will:
· Add a global default error handler to a Mule application.
· Compare the difference between On Error Propagate and On Error Continue scopes.
· Reference a global error handler from a flow.
· Add a sequence of event processors to a Try scope.
· Set match conditions in error scopes.
· Create custom error mappings.
Scenario
A Mule application retrieves flight data from various flight services. You need to add robust error handling to the Mule application.
Import the starting project
Import /files/module10/flights-mod10-error-handling-starter.jar (in the MUFundamentals4.x DIY Files zip that you can download from the Course Resources) into Anypoint Studio.
Review the flows in the starting project
Review the structure of the gatherdata.xml and standardize.xml files:
· The Mule application has only one endpoint and expects a URI parameter named destination and a query parameter named maxPrice.
· Requests are received by the flights flow in gatherdata.xml.
· This flow uses a Scatter-Gather to concurrently retrieve flight data.
· In the standardize.xml file, getUnited uses a filter to filter invalid payloads returned if the United service is given an invalid destination.
· In the standardize.xml file, getDelta uses DataWeave to filter null payloads returned if the Delta service is given an invalid destination.
Verify the default out-of-the-box error handling behavior
Run the project and make a GET request to http://locahost:8081/flights/MUA?maxprice=1000 and verify that an error is thrown. Debug the Mule application, step through the flows, and note how the error is handled.
Answer the following questions
· What type of error is logged by the default error handler?
· What is the error status code?
· What type of error scope is used by the default error handler (On Error Propagate or On Error Continue)?
Create a global default error handler
In global.xml, add a global error handler with an On Error Propagate scope to catch any error thrown in the Mule application. Inside the handler, log a message that includes the error's namespace and identifier.
Make the same GET request, step through the flows, and note how the error is handled.
Answer the following questions
· How many errors are thrown?
· What error is thrown first?
· Why are there different types of errors thrown?
Add more details to the HTTP Listener error response
In gatherdata.xml, set the error response in the HTTP Listener to the following data structure:
#[output application/json ---
{
errorObject: error.errorType,
errorNamespace: error.errorType.namespace,
errorID: error.errorType.identifier,
description: error.description
}
]
Add an error handler to a flow
In standardize.xml, add error scopes to the getAmerican flow to handle errors in the following order:
· For HTTP:BAD_REQUEST errors, use an On Error Continue scope and return an empty array.
· For DataWeave transformation errors, use an On Error Propagate scope.
· For all other errors, use an On Error Propagate scope.
Add Loggers to each error scope to identify what error type was thrown. Make the same GET request, step through the flows, and note how the error is handled.
Answer the following questions
· What is the result of the GET request?
· What is the status code returned from the GET request?
· Did the Logger in getAmerican get executed? What is the reason for this behavior?
Create a flow level error handler
Extract the error scopes you defined in the getAmerican flow into a standalone error handler inside standardize.xml. Name the error handler standardize-error-handler.
Add an error handler reference for the getAmerican, getUnited, and getDelta flows to reference the common error handler in standardize.xml.
Note: In Anypoint Studio version 7.1.4, there is no error handler reference in the Mule Palette. You need to directly reference the error handler using the following XML code:
<error-handler ref="standardize-error-handler"/>
Catch web service errors in a Try scope
In the getDelta flow, add a Try scope after the Build SOAP Request Transform Message component. Move the Web Service Consume operation and the Logger into the Try scope. To intentionally throw an error in the Consume operation, change the SOAP operation from findFlight to findFlights.
Add error scopes to the Try scope in the following order:
· For findFlights operation errors, use an On Error Propagate scope.
· For WSC:BAD_REQUEST, WSC:BAD_RESPONSE, WSC:RETRY_EXHAUSTED, and WSC:TIMEOUT errors, use an On Error Continue scope and return an empty object.
· For all other WSC errors, use an On Error Propagate scope.
Add Loggers to each error scope to identify what error type was thrown. Make the same GET request, step through the flows, and note how the error is handled.
Make a GET request to http://localhost:8081/flights/SFO?maxprice=1000 and step through the flows and observe the behavior.
Answer the following questions
· What is the result of the GET request?
· What is the status code returned from the GET request?
· Did the Logger in the Try scope (not the Loggers in its error scopes) get executed? What is the reason for this behavior?
Add custom error mapping and catch the custom error
In the getDelta flow in standardize.xml, add a custom error mapping for errors generated by the Web Service Consume operation; map the WSC:BAD_REQUEST error to DELTA:BAD_WSC_REQUEST.
In the standardize-error-handler, add two additional On Error Propagate scopes to handle errors of type WSC:BAD_REQUEST and DELTA:BAD_WSC_REQUEST. Place the WSC:BAD_REQUEST error scope before the DELTA_BAD_WSC_REQUEST error scope. Add Loggers to each error scope to log what error type is thrown.
Make the same GET request and step through the flow and observe the behavior.
Answer the following questions
· What is the result of the GET request?
· What is the status code returned from the GET request?
· Which error handler caught the bad SOAP request?
· What happens to the request if the DELTA:BAD_WSC_REQUEST error scope is changed to an On Error Continue scope?
Verify your solution
Load the solution /files/module10/flights-mod10-error-handling-solution.jar (in the MUFundamentals4.x DIY Files zip that you can download from the Course Resources) and compare your solution.